package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.entity.Reservation;
import com.zzyl.mapper.CustomerReservationMapper;
import com.zzyl.service.CustomerReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.ReservationCountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;

@Service
public class CustomerReservationServiceImpl implements CustomerReservationService {

    @Autowired
    private CustomerReservationMapper customerReservationMapper;

    //查询取消次数
    @Override
    public Integer cancelledCount() {
        Long userId = UserThreadLocal.getUserId();
        //查询手机号
        //String mobile = customerReservationMapper.findMobile(userId);

        //查询当天,查询当天的开始时间
        LocalDateTime begin =LocalDate.now().atStartOfDay();
        LocalDateTime end = LocalDate.now().atTime(23,59,59);
        //查询该手机号下取消的次数
        Integer count = customerReservationMapper.cancellationCount(userId,begin,end);

        return count;
    }

    //创建预约
    @Override
    public void createReservation(Reservation reservation) {
        reservation.setStatus(0);
        customerReservationMapper.createReservation(reservation);
    }

    //更新预约
    @Override
    public void updateReservation(Long id) {
        customerReservationMapper.updateReservation(id);
    }

    //分页查询
    @Override
    public PageResponse<Reservation> findByPage(Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);

        List<Reservation> list = customerReservationMapper.findByPage(status);
        Page<Reservation> page = (Page<Reservation>) list;

        PageResponse<Reservation> reservationPageResponse = new PageResponse<>();
        reservationPageResponse.setRecords(page.getResult());
        reservationPageResponse.setTotal(page.getTotal());
        reservationPageResponse.setPages((long) page.getPages());
        reservationPageResponse.setPageSize(page.getPageSize());
        reservationPageResponse.setPage(page.getPageNum());

        //PageResponse<Reservation> of = PageResponse.of(page, Reservation.class);
        return reservationPageResponse;

    }

    /**
     * 根据指定时间查询预订数量。
     *
     * @param time 指定时间的毫秒数，需要转换为上海时区的LocalDateTime。
     * @return 返回一个包含每30分钟时间段内可预订数量的列表。
     */
    @Override
    public List<ReservationCountVo> countByTime(Long time) {
        // 将传入的毫秒数转换为上海时区的LocalDateTime，并设置为当天的08:00开始时间
        LocalDateTime begin =Instant.ofEpochMilli(time).atZone(ZoneId.of("Asia/Shanghai")).toLocalDateTime().plusHours(8);
        // 计算结束时间为当天的22:00
        LocalDateTime end =begin.plusHours(18);
        // 查询从开始时间到结束时间内的预订数量
        List<ReservationCountVo> list =customerReservationMapper.countByTime(begin,end);
        // 用于存储计算结果的列表
        ArrayList<ReservationCountVo> result = new ArrayList<>();
        // 遍历从08:00到22:00，每30分钟一个时间段，计算可预订数量
        while (begin.isBefore(end)|| begin.equals(end)){
            // 初始化每30分钟时间段的可预订数量为6
            ReservationCountVo reservationCountVo = new ReservationCountVo(6, begin);
            // 根据查询结果，更新该时间段的实际可预订数量
            for(ReservationCountVo countVo : list){
                if(countVo.getTime().equals(begin)){
                    reservationCountVo.setCount(6-countVo.getCount());
                    break;
                }
            }
            // 将计算结果添加到结果列表中
            result.add(reservationCountVo);
            // 移动到下一个30分钟时间段
            begin = begin.plusMinutes(30);
        }
        return result;
    }


    @Override
    public void executeTask() {
        LocalDateTime now = LocalDateTime.now();
        customerReservationMapper.executeTask(now);
    }
}
